package com.ruoyi.system.controller;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.service.impl.SysUserServiceImpl;
import com.ruoyi.system.util.JudgeResultObject;
import org.apache.ibatis.annotations.Update;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.IndicatorJudgement;
import com.ruoyi.system.service.IIndicatorJudgementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2024-08-07
 */
@RestController
@RequestMapping("/system/judgement")
public class IndicatorJudgementController extends BaseController
{
    @Autowired
    private IIndicatorJudgementService indicatorJudgementService;

    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:judgement:list')")
    @GetMapping("/list")
    public TableDataInfo list(IndicatorJudgement indicatorJudgement)
    {
        startPage();
        if(getLoginUser().getUser().isAdmin()||getLoginUser().getUser().isTC()) {
            indicatorJudgement.setUserId(null);
        }else{
            indicatorJudgement.setUserId(getLoginUser().getUserId());
        }
        List<IndicatorJudgement> list = indicatorJudgementService.selectIndicatorJudgementList(indicatorJudgement);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:judgement:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IndicatorJudgement indicatorJudgement)
    {
        List<IndicatorJudgement> list = indicatorJudgementService.selectIndicatorJudgementList(indicatorJudgement);
        ExcelUtil<IndicatorJudgement> util = new ExcelUtil<IndicatorJudgement>(IndicatorJudgement.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:judgement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(indicatorJudgementService.selectIndicatorJudgementById(id));
    }

    /**
     * 新增【请填写功能名称】
     */


    @PreAuthorize("@ss.hasPermi('system:judgement:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<IndicatorJudgement> indicatorJudgements) {
        int cnt = 0;
            for (IndicatorJudgement indicatorJudgement : indicatorJudgements) {
                System.out.println(indicatorJudgement.getUserId());
                if(indicatorJudgement.getUserId() != null)
                    indicatorJudgement.setCreateUser(sysUserServiceImpl.selectUserById(indicatorJudgement.getUserId()).getUserName());
                if(indicatorJudgement == null)
                    indicatorJudgement.setStatus(2L);
                // 设置当前时间
                indicatorJudgement.setCreateTime(new Date());
                indicatorJudgementService.insertIndicatorJudgement(indicatorJudgement);
                cnt++;
            }
        return toAjax(cnt);
    }


    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:judgement:mark')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IndicatorJudgement indicatorJudgement)
    {
        return toAjax(indicatorJudgementService.updateIndicatorJudgement(indicatorJudgement));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:judgement:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(indicatorJudgementService.deleteIndicatorJudgementByIds(ids));
    }

//    @PreAuthorize("@ss.hasPermi('system:judgement:audit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping("/audit/{id}")
    public AjaxResult update(@RequestBody HashMap<Long, Double> resMap, @PathVariable Long id)
    {
        return toAjax(indicatorJudgementService.updateIndicatorJudgementStatus(id, resMap));
    }

    @PreAuthorize("@ss.hasPermi('system:judgement:calculate')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping("/cal/{ids}")
    public AjaxResult calculateSubjectiveWeight(@PathVariable Long[] ids)
    {
//        if(!getLoginUser().getUser().isAdmin()){
//            return error("非管理员无法计算");
//        }
        JudgeResultObject resultObject;
        try{
            resultObject = indicatorJudgementService.calculateSubjectiveWeight(ids);
        }catch (Exception e){
            return error(e.getMessage());
        }
        return success(resultObject);
    }
}
